package com.heima.dfs.controller;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/dfs")
public class UploadController {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    /**
     * 文件下载方法
     * List<String>urls图片地址 http：//192.168.211.136/group1/M00/00/01/XXX.jpg
     * return List<byte[]>
     * @return
     */
    @PostMapping(value = "/list/download")
    public List<byte[]> download(@RequestParam List<String> images)throws Exception{
      List<byte[]> imageBytes = new ArrayList<>();
        for (String image : images) {
            if (image.startsWith("http://")){
                image.replace(fdfsWebServer.getWebServerUrl()+"group1/","");
            }
            byte[] bytes = fastFileStorageClient.downloadFile("group1", image, new DownloadByteArray());
            imageBytes.add(bytes);
        }
        return imageBytes;
    }



    /**
     * html静态页文件上传
     * 参数 String 文件的文本格式
     * 1.接收文本
     * 2.将文本转换为输入流
     * 3.将输入流写入到FastDFS
     * 4.将文件访问地址返回到Canal
     */

    @PostMapping(value="/html/upload")
    public ResponseResult uploadHtml(@RequestParam String content)throws Exception{
        //将文本转换为输入流
        byte[] bytes = content.getBytes();
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        //将输入流写入FastDFS
        StorePath storePath = fastFileStorageClient.uploadFile(is,
                bytes.length,
                "html",
                null);

        String url = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        return ResponseResult.okResult(url);

    }





    /**
     * 文件上传
     */
    @PostMapping(value = "/upload")
    public ResponseResult upload(MultipartFile file) throws Exception {
        //获取文件的名字

        //获取文件的输入流
        //上传到FastDfs
        //返回路劲地址响应给前端
        //StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MetaData> metaDataSet);

        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), StringUtils.getFilenameExtension(file.getOriginalFilename()), null);
        String fullPath = storePath.getFullPath();
        String url = fdfsWebServer.getWebServerUrl() + fullPath;
        return ResponseResult.okResult(url);

    }

}
